package com.zaofan.utils;

import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.zaofan.bean.OrderInfo;
import com.zaofan.service.OrderInfoService;
import com.zaofan.service.ProductService;

public class TaskServletContextListner implements ServletContextListener {
	private static Timer timer1 = null;
	private static Timer timer2 = null;
	private ProductService productService = new ProductService();
	private OrderInfoService orderService = new OrderInfoService();

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		if (timer1 != null) {
			timer1.cancel();
		}
		if (timer2 != null) {
			timer2.cancel();
		}
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {

		Calendar date = Calendar.getInstance();
		date.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 0, 0, 1);
		date.add(Calendar.DATE, 1);
		long daySpan = 24 * 60 * 60 * 1000;
		Timer timer1 = new Timer();
		timer1.schedule(new TimerTask() {
			public void run() {
				System.err.println("执行");
				//productService.updateDateSellCount();
			}
		}, date.getTime(), daySpan);

		Calendar date2 = Calendar.getInstance();
		date2.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date.get(Calendar.DATE), 8, 30, 0);
		date2.add(Calendar.DATE, 1);
		timer2 = new Timer();
		timer2.schedule(new TimerTask() {
			public void run() {
				//sendOrderUser();
			}
		}, date2.getTime(), daySpan);
	}

	private void sendOrderUser() {
		List<OrderInfo> orders = orderService.exportTodaySendUserMobile();
		for (OrderInfo info : orders) {
			JPushUtils.sendCompleteOrder(info, info.getAccountMobile());
		}
	}

	public static void main(String[] args) {

	}

}
